package marke.barak.windows.composit;

import java.util.ArrayList;
import java.util.List;

import marek.barak.classes.Data;
import marek.barak.exceptions.InvalidTypeOfRequestException;
import marek.barak.windows.main.main;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.DisposeEvent;

public class TypeOfRequest extends Composite {
	private Text textRFA;
	private List<Text> texts;
	private List<Label> labels;
	private List<Button> buttons;
	private List<DateTime> dateTime;
	
	private void clearItems(){
		if(texts.size()>0){
			for(Text t:texts){
				t.dispose();
			};
			texts.clear();
		}
		if(labels.size()>0){
			for(Label l:labels){
				l.dispose();
			};
			labels.clear();
		}
		if(buttons.size()>0){
			for(Button b: buttons){
				b.dispose();
			}
			buttons.clear();
		}
		if (dateTime.size()>0){
			for(DateTime d: dateTime){
				d.dispose();
			}
			dateTime.clear();
		}
		
	}
	
	private void lineItem(){
		
		clearItems();
			
		
		labels.add(new Label(this,SWT.NONE));
		labels.get(0).setFont(SWTResourceManager.getFont("Times New Roman Baltic", 9, SWT.BOLD));
		labels.get(0).setBounds(10, 49, 20, 20);
		labels.get(0).setText("LI");
		labels.add(new Label(this,SWT.NONE));
		labels.get(1).setFont(SWTResourceManager.getFont("Times New Roman Baltic", 9, SWT.BOLD));
		labels.get(1).setBounds(130, 49, 60, 20);
		labels.get(1).setText("LEVEL");
		
		texts.add(new Text(this,SWT.BORDER));
		texts.get(0).setBounds(labels.get(0).getLocation().x,labels.get(0).getLocation().y + 25,100,20);
		texts.add(new Text(this,SWT.BORDER));
		texts.get(1).setBounds(labels.get(1).getLocation().x,labels.get(1).getLocation().y + 25,100,20);
		}
	private void rfaOnline(){
		clearItems();
		
		buttons.add(new Button(this,SWT.CHECK));
		buttons.get(0).setText("RFA");
		buttons.get(0).setBounds(10, 49, 50, 20);
		
		buttons.add(new Button(this,SWT.CHECK));
		buttons.get(1).setText("MRS");
		buttons.get(1).setBounds(70,49,50,20);
		
		buttons.add(new Button(this,SWT.CHECK));
		buttons.get(2).setText("EPIC");
		buttons.get(2).setBounds(130,49,50,20);
		
	}
	private void email(){
		clearItems();
		texts.add(new Text(this,SWT.BORDER));
		texts.get(0).setBounds(10,49,100,20);
		texts.get(0).setText("Who");
		
		texts.add((new Text(this,SWT.BORDER)));
		texts.get(1).setBounds(130,49,100,20);
		texts.get(1).setText("Subject");
		
	}
	private void tir(){
		clearItems();
		texts.add(new Text(this,SWT.BORDER));
		texts.get(0).setBounds(10,49,100,20);
		
	}
	private void mn(){
		clearItems();
		texts.add(new Text(this,SWT.BORDER));
		texts.get(0).setBounds(10,49,100,20);
	}
	private void portfolio(){
		clearItems();
		dateTime.add(new DateTime(this,SWT.BORDER));
		dateTime.get(0).setBounds(10,49,87,21);
		
		texts.add(new Text(this,SWT.BORDER));
		texts.get(0).setBounds(100,49,100,20);
		texts.get(0).setText("Point");
	}
	private void call(){
		clearItems();
		texts.add(new Text(this,SWT.BORDER));
		texts.get(0).setBounds(10,49,100,20);
		texts.get(0).setText("Which");
	}
	private void other(){
		clearItems();
		texts.add(new Text(this,SWT.BORDER));
		texts.get(0).setBounds(10,49,200,20);
	}
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public TypeOfRequest(Composite parent, int style) {
		super(parent, style);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				main.c = new MeasurementDataLoad(getShell(), SWT.FILL);
				main.c.setSize(500, 500);
				main.c.getShell().setSize(620,580);
		
			}
		});
		texts = new ArrayList<Text>();
		labels = new ArrayList<Label>();
		buttons = new ArrayList<Button>();
		dateTime = new ArrayList<DateTime>();
		
		final Combo combo = new Combo(this, SWT.NONE);
		combo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				switch(combo.getSelectionIndex()){
				case 0:
					lineItem();
					break;
				case 1:
					rfaOnline();
					break;
				case 2:
					email();
					break;
				case 3:
					tir();
					break;
				case 4:
					mn();
					break;
				case 5:
					portfolio();
					break;
				case 6:
					call();
					break;
				case 7:
					other();
					break;
			}
		}});
		combo.setItems(new String[] {"LINE ITEM", "RFA ONLINE", "EMAIL", "TIR", "MN", "PORTFOLIA", "CALL", "OTHER"});
		combo.setBounds(10, 10, 93, 21);
		
		textRFA = new Text(this, SWT.BORDER);
		textRFA.setBounds(309, 10, 130, 19);
		
		Label lblRfa = new Label(this, SWT.NONE);
		lblRfa.setFont(SWTResourceManager.getFont("Times New Roman Baltic", 9, SWT.BOLD));
		lblRfa.setBounds(254, 12, 49, 13);
		lblRfa.setText("RFA#");
		
		Button btnNext = new Button(this, SWT.NONE);
		btnNext.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try{
					if(textRFA.getText().length()>0){
						switch(combo.getSelectionIndex()){
						case 0:
							if(texts.get(0).getText().length()>0 && texts.get(1).getText().length()>0)
								Data.Builder.setTypeOfRequest("LINE ITEM - LI: "+ texts.get(0).getText() + " Level: "+texts.get(1).getText());
							else
								throw new InvalidTypeOfRequestException("Fill booth fields");
							break;
						case 1:
							if(buttons.get(0).getSelection()&&buttons.get(1).getSelection() || 
									buttons.get(1).getSelection()&&buttons.get(2).getSelection() ||
									buttons.get(0).getSelection()&&buttons.get(2).getSelection())
								throw new InvalidTypeOfRequestException("Choose only one option");
							else{
								
								String rfaOnline =(buttons.get(0).getSelection()? "RFA ":(buttons.get(1).getSelection()? "MRS":(buttons.get(2).getSelection()?"EPIC":null )));
								if(rfaOnline ==null)
									throw new InvalidTypeOfRequestException("Choose one option");
								Data.Builder.setTypeOfRequest("RFA ONLINE - "+ rfaOnline);
							};
							break;
						case 2:
							if(texts.get(0).getText().length()>0 && texts.get(1).getText().length()>0)
								Data.Builder.setTypeOfRequest("EMAIL - Who: "+texts.get(0).getText()+ " Subject: "+texts.get(1).getText());
							else
								throw new InvalidTypeOfRequestException("Fill booth fields.");
							break;
						case 3:
							if(texts.get(0).getText().length()>0)
								Data.Builder.setTypeOfRequest("TIR: "+ texts.get(0).getText());
							else
								throw new InvalidTypeOfRequestException("Fill the empty field");
							break;
						case 4:
							if(texts.get(0).getText().length()>0)
								Data.Builder.setTypeOfRequest("MN: "+texts.get(0).getText());
							else
								throw new InvalidTypeOfRequestException("Fill the empty field");
							break;
						case 5:
							if(texts.get(0).getText().length()>0)
								Data.Builder.setTypeOfRequest("PORTFOLIO - Date: "+ dateTime.get(0).getDay()+"."+dateTime.get(0).getMonth()+"."+dateTime.get(0).getYear()+" Subject: "+texts.get(0).getText());
							else
								throw new InvalidTypeOfRequestException("Fill the Point field");
							break;
						case 6:
							if(texts.get(0).getText().length()>0)
								Data.Builder.setTypeOfRequest("CALL: "+texts.get(0).getText());
							else
								throw new InvalidTypeOfRequestException("Fill the Which field");
							break;
						case 7:
							if(texts.get(0).getText().length()>0)
								Data.Builder.setTypeOfRequest("OTHER: "+ texts.get(0).getText());
							else
								throw new InvalidTypeOfRequestException("Fill the empty field");
						}
						main.c.dispose();
					}
				else
					throw new InvalidTypeOfRequestException("Enter RFA text");
				}catch(InvalidTypeOfRequestException ex){
					MessageBox msg = new MessageBox(getShell(),SWT.ICON_ERROR | SWT.ABORT);
					msg.setMessage(ex.getMessage());
					msg.setText("Error");
					msg.open();
				}
			}
		});
		btnNext.setBounds(371, 267, 68, 23);
		btnNext.setText("Next");
		
		
		

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
